<?php

/**
 * 实名认证
 */

namespace app\member\common;

class RealCommon extends \app\member\common\MemberCommon {


    protected function index() {
        $info = target('member/MemberReal')->getWhereInfo([
            'A.user_id' => $this->userInfo['user_id']
        ]);
        if (!isPost()) {
            $this->pageInfo['name'] = '实名认证';
            $this->setMeta('实名认证');
            $this->setCrumb([
                [
                    'name' => '实名认证',
                    'url' => url()
                ]
            ]);
            $this->assign('bankList', target('member/PayBank')->loadList());
            $this->assign('info', $info);
            $this->memberDisplay();
        } else {
            if ($info['status'] == 1) {
                $this->error('您的申请已提交，请勿重复提交！');
            }
            if ($info['status'] == 2) {
                $this->error('您的实名制认证已成功，无法再次修改！');
            }
            $post = request('post');
            if (empty($post['code']) || empty($post['name']) || empty($post['idcard']) || empty($post['tel'])) {
                $this->error('输入信息不完整，请重新输入！');
            }
            if (!$this->isIdcard($post['idcard'])) {
                $this->error('身份证号码输入不正确！');
            }
            $label = $_POST['label'];
            $bankInfo = target('member/PayBank')->getWhereInfo([
                'label' => $label
            ]);
            if (empty($bankInfo)) {
                $this->error('开户银行不存在！');
            }

            $type = request('post', 'valtype', 0, 'intval');
            if (!$type) {
                $receive = $this->userInfo['tel'];
            } else {
                $receive = $this->userInfo['email'];
            }

            if (empty($receive)) {
                $this->error('该验证方式未绑定，请使用其他验证方式！');
            }

            if (!target('member/Member', 'Service')->checkVerify($receive, $post['code'], 2)) {
                $this->error(target('member/Member', 'Service')->getError());
            }

            $model = target('member/MemberReal');
            $model->beginTransaction();

            $data = [
                'user_id' => $this->userInfo['user_id'],
                'tel' => $post['tel'],
                'name' => $post['name'],
                'idcard' => $post['idcard'],
                'create_time' => time(),
                'status' => 1
            ];

            if (empty($info)) {
                if (!$model->add($data)) {
                    $model->rollBack();
                    $this->error('认证资料提交失败！');
                }
            } else {
                $data['real_id'] = $info['real_id'];
                if (!$model->edit($data)) {
                    $model->rollBack();
                    $this->error('认证资料修改失败！');
                }
            }

            $cardInfo = target('member/PayCard')->getWhereInfo(['A.user_id' => $this->userInfo['user_id']]);

            $data = [
                'user_id' => $this->userInfo['user_id'],
                'name' => $bankInfo['name'],
                'label' => $label,
                'account' => $post['account'],
                'account_tel' => $post['tel'],
                'account_name' => $post['name'],
                'account_bank' => $post['account_bank']
            ];

            if ($cardInfo) {
                $data['card_id'] = $cardInfo['card_id'];
            }

            if (!target('member/PayCard')->saveData($cardInfo ? 'edit' : 'add', $data)) {
                $model->rollBack();
                $this->error(target('member/PayCard')->getError());
            }

            $model->commit();
            $this->success('认证资料提交成功，请耐心等待审核！');

        }
    }

    protected function getCode() {
        if (!$this->getImgCode()->check(request('post', 'imgcode'))) {
            $this->error('图片验证码输入不正确!');
        }
        $type = request('post', 'valtype', 0, 'intval');
        if (!$type) {
            $receive = $this->userInfo['tel'];
        } else {
            $receive = $this->userInfo['email'];
        }

        if (empty($receive)) {
            $this->error('该验证方式未绑定，请使用其他验证方式！');
        }

        if (!target('member/Member', 'service')->getVerify($receive, '', 0, 2)) {
            $this->error(target('member/Member', 'service')->getError());
        }
        $this->success('验证码已发送,请注意查收!');
    }

    protected function isIdcard($id) {
        $id = strtoupper($id);
        $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
        $arr_split = array();
        if (!preg_match($regx, $id)) {
            return FALSE;
        }
        if (15 == strlen($id)) {
            $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

            @preg_match($regx, $id, $arr_split);
            $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
            if (!strtotime($dtm_birth)) {
                return FALSE;
            } else {
                return TRUE;
            }
        } else {
            $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
            @preg_match($regx, $id, $arr_split);
            $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
            if (!strtotime($dtm_birth)) {
                return FALSE;
            } else {
                $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
                $sign = 0;
                for ($i = 0; $i < 17; $i++) {
                    $b = (int)$id{$i};
                    $w = $arr_int[$i];
                    $sign += $b * $w;
                }
                $n = $sign % 11;
                $val_num = $arr_ch[$n];
                if ($val_num != substr($id, 17, 1)) {
                    return FALSE;
                } else {
                    return TRUE;
                }
            }
        }

    }

}